Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "",
    createTime: '',
    thumbUpTimes: '',
    browseTimes: '',
    expandList: [],
    list: [],
    zan:true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取文章列表传过来的id
    this.setData({
      id: options.id
    })
    // console.log(this.data.id) 
    this.getArticleDetail();
  },
  //详情
  getArticleDetail() {
    var that = this;
    wx.request({
      url: 'http://39.105.120.99:80/book/api/content/detail',
      data: { id: this.data.id },
      header: {
        'content-type': 'application/json'
      },
      method: 'get',
      dataType: 'json',
      // responseType: 'text',
      success: function (res) {
        console.log(res.data.data.list)
        that.setData({
          title: (res.data.data.title),
          createTime: (res.data.data.createTime),
          thumbnail: (res.data.data.thumbnail),
          thumbUpTimes: (res.data.data.thumbUpTimes),
          browseTimes: (res.data.data.browseTimes),
          expandList: that.data.expandList.concat(res.data.data.expandList),//拓展阅读列表 .concat 是将后台获取来的数据 拼接到 上面data中定义的数组中
          list: that.data.list.concat(res.data.data.list)
        })
      }

    })

  },
  //文章点赞
  praiseTap() {
    var that = this;
    wx.request({
      url: 'http://39.105.120.99:80/book/api/content/thumbUp',
      data: {
        sessionid: "sessionid",
        contentId: that.data.id
      },
      success(res) {
        // console.log(res.data.data.thumbUpTimes)
        if (res.data.code === 'USR000') {
          that.setData({
            thumbUpTimes: res.data.data.thumbUpTimes,
            zan :false
          })
        } else {
          that.setData({
            zan: false 
          })
          console.log(res.data.msg)
        }

      }

    })

  },
  //评论点赞
  commentLike(e) {
    // console.log(e.currentTarget)
    var that = this;
    wx.request({
      url: 'http://39.105.120.99:80/book/api/comment/thumbUp',
      data: {
        sessionid: 'sessionid',
        commentId: e.currentTarget.id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: "get",
      success(res) {
        // console.log(res.data.data.thumbUpTimes)
        if (res.data.code === "USR000") {
          var num = e.currentTarget.dataset.index;
          console.log(num)
          var like = "list[" + num + "].thumbUpTimes";
          console.log(like)
          var show = "list[" + num + "].isshow";
          console.log(show)
          var newData = that.data.list[num].thumbUpTimes;
          if (that.data.list[num].thumbUpTimes == 0){
            that.setData({
              [like]: ++newData,
              [show]:true
            })
          }else{
            that.setData({
              [like]:--newData,
              [show]:false
            })
          }
        } else {
          console.log(res.data.msg)
        }

      }
    })

  },
  //跳转到写评论页面
  skipComment(e) {
    // console.log(e.currentTarget.id)
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '../comment/comment?id='+ id
    })

  },
  //跳转拓展阅读详情
  onRead(e){
    // console.log(e)
    var Id = e.currentTarget.id;
    wx.navigateTo({
      url: './read-detail/read-detail?id=' + Id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})